<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <NewPageFilter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
      :filter-btn="filterInfo.btn"
    >
    </NewPageFilter>
    <dialog-table
      ref="table"
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :listen-height="false"
      :tab-index="false"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :selectable="selectable"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :checkedList="checkBox ? checkedList : []"
      :pager="false"
      :spanMethod="spanMethod"
      :hasRowTableClickCallback="true"
      @handleEvent="handleEvent"
    />
  </page-dialog>
</template>

<script setup>
const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: true,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  selectable: {
    type: Function,
  },
  api: {
    type: Function,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  checkedList: {
    type: Array,
    default: function () {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Array,
    default: function () {
      return []
    },
  },
  changeContract: {
    type: Boolean,
    default: false,
  },
})

// 过滤相关配置
const filterInfo = ref({
  query: proxy.$fn.deepClone(props.query),
  copyQuery: {},
  list: [
    {
      componentType: 'input',
      label: '品种名称',
      value: 'varietiesNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '厂家',
      value: 'brandNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '产品编码',
      value: 'productCodeLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '产品名称',
      value: 'commonNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '规格',
      value: 'standardLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '计量单位',
      value: 'unitNameLike',
      event: search,
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
})

// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  copyData: [],
  selections: [],
  fieldList: [
    { label: '序号', value: 'index', width: '40' },
    { label: '品种名称', value: 'varietiesName' },
    { label: '厂家', value: 'brandName' },
    { label: '产品编码', value: 'productCode' },
    { label: '产品名称', value: 'commonName' },
    { label: '规格', value: 'standard' },
    { label: '计量单位', value: 'unitName' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '新增品种',
  btLoading: false,
  width: '800px',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList()
  })
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})

function spanMethod({ row, column, rowIndex, columnIndex }) {
  let keys = [
    'index',
    'index',
    'varietiesName',
    'brandName',
    '',
    '',
    '',
    '',
    '',
  ]
  // 注意:字段列表要按表格排列的顺序排列,如果后续字段不再需要进行行合并,可以省略后面的字段,如果中间某字段要跳过合并,要用空字符串顶位
  return proxy.$fn.spanMethod(tableInfo.value.data, keys, rowIndex, columnIndex)
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  search()
}
// 获取列表
function getList() {
  tableInfo.value.api = props.api
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  const tempData = []
  tableInfo.value.data.forEach((item) => {
    const tempBool = selectRow.value.some((item2) => {
      if (item.index === item2.index) {
        return true
      }
    })
    if (tempBool) {
      tempData.push(item)
    }
  })
  console.log(tempData)
  emits('callback', tempData)
  handleClose()
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !(
          selectRow.value &&
          (selectRow.value.length || selectRow.value.id)
        )
      }
      break
    case 'rowTableClick':
      selectRow.value = data
      tableInfo.value.data.some((item) => {
        if (item.id === data.id) {
          proxy.$refs.table.toggleRowSelection(item)
          return true
        }
      })
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !(
          selectRow.value &&
          (selectRow.value.length || selectRow.value.id)
        )
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
</script>

<style scoped></style>
